package com.e3.sysmg.config.dao;

import java.util.ArrayList;
import java.util.List;

import com.e3.core.base.SuperDAO;
import com.e3.sysmg.config.entity.ParameterEntity;
import com.e3.sysmg.config.model.ParameterQC;

public class ParameterDAO extends SuperDAO {
	
	private String selectSQL = "SELECT t FROM ParameterEntity t";
	private String countSQL = "SELECT count(t) FROM ParameterEntity t";
	
	@SuppressWarnings("rawtypes")
	public List<Long> getParameterIdsByCondition(ParameterQC qc) {
		String selectSQL = "SELECT t.id FROM ParameterEntity t";
		qc.setPrefix("t");
		String whereSQL = qc.getWhereSQL();
		if (whereSQL != null && whereSQL.length() > 0) {
			selectSQL = selectSQL + " WHERE " + whereSQL;
		}
		List result = executeHQLQuery(selectSQL, null);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>(result.size());
			for (Object object : result) {
				ids.add((Long)object);
			}
			return ids;
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	public List<Long> getPagingParameterIdsByCondition(ParameterQC goodsQC,int start,int max) {
		String sql = "SELECT t.id FROM ParameterEntity t";
		if(goodsQC != null){
			goodsQC.setPrefix("t");
			String whereSQL = goodsQC.getWhereSQL();
			if (whereSQL != null && whereSQL.length() > 0) {
				sql = sql + " WHERE " + whereSQL;
			}
		}
		List result = executePagingHQLQuery(sql, null,start,max);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>(result.size());
			for (Object object : result) {
				ids.add((Long)object);
			}
			return ids;
		}
		return null;
	} 


	public List<ParameterEntity> getAllParameter(){
		return this.find(selectSQL);
	}

	public ParameterEntity getParameterByName(String name) {
		String whSql = " WHERE t.name = ?";
		String sql = selectSQL + whSql;
		List<ParameterEntity> entities = this.find(sql, name);
		return (entities == null || entities.size() == 0) ? null : entities.get(0);
	}
	
	public void deleteParameterByIds(List<Long> ids) {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		Long[] idArray = new Long[ids.size()];
		ids.toArray(idArray);
		this.deleteAllBySingleField(ParameterEntity.class, "id", idArray);
	}
	
	public long getParameterSizeByName(String name){
		String whSql = " WHERE t.name = ?";
		String sql = countSQL + whSql;
		return counter(sql,name);
	}

	public long counterByCondition(ParameterQC qc){
		String sql = "SELECT count(t) FROM ParameterEntity t";
		if(qc!=null){
			qc.setPrefix("t");
			String whereSQL = qc.getWhereSQL();
			if (whereSQL != null && whereSQL.length() > 0) {
				sql = sql + " WHERE " + whereSQL;
			}
		}
		
		return this.counter(sql);
	}

}
